osal: fix rtthread usb_osal_thread_delete() API#288
osal: fix rtthread usb_osal_thread_delete() API#288sakumisu merged 1 commit intocherry-embedded:masterfrom
Conversation
Signed-off-by: Zhihong Chen <zhihong.chen@hpmicro.com>
WalkthroughThe changes modify the Changes
Sequence DiagramsequenceDiagram
participant Caller
participant Thread
participant OSAL
Caller->>OSAL: usb_osal_thread_delete(NULL)
OSAL->>Thread: rt_thread_self()
OSAL->>Thread: rt_thread_control(close)
Note over OSAL,Thread: Closes current thread
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
osal/usb_osal_rtthread.c (1)
29-30: Add documentation for NULL thread handling.Please add a comment explaining that when
threadis NULL, the function terminates the current thread. This behavior should be documented for API users.void usb_osal_thread_delete(usb_osal_thread_t thread) { + /* If thread is NULL, terminate the current thread */ if (thread == NULL) {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
osal/usb_osal_rtthread.c(1 hunks)
🔇 Additional comments (1)
osal/usb_osal_rtthread.c (1)
29-30: Consider thread safety implications of self-termination.Self-thread termination using
RT_THREAD_CTRL_CLOSEcould be unsafe if:
- Called from within a critical section
- Thread holds locks or resources
- Thread is processing important operations
Consider adding safety checks or documenting these limitations.
Let's verify the usage of critical sections and locks in the codebase:
✅ Verification successful
Thread deletion implementation follows safe practices
The current implementation of thread self-termination is safe because:
- Only used at well-defined exit points during cleanup
- Not called within critical sections or while holding locks
- Follows standard RT-Thread practices for thread cleanup
The review comment's concerns about thread safety implications are addressed by the current implementation's careful placement of thread deletion calls.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Search for critical sections and mutex operations that might interact with thread deletion rg -A 2 "usb_osal_thread_delete|rt_thread_control.*RT_THREAD_CTRL_CLOSE" --type c rg "usb_osal_(enter|leave)_critical|usb_osal_mutex_(take|give)" --type cLength of output: 9585
Script:
#!/bin/bash # Get more context around the thread deletion code rg -B 10 -A 10 "rt_thread_control.*RT_THREAD_CTRL_CLOSE" --type c # Find where usb_osal_thread_delete is called from with context rg -B 5 -A 5 "usb_osal_thread_delete" --type cLength of output: 12817
| rt_thread_t self = rt_thread_self(); | ||
| rt_thread_control(self, RT_THREAD_CTRL_CLOSE, RT_NULL); |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Validate return value of rt_thread_self().
The return value of rt_thread_self() should be validated to ensure we don't pass a NULL thread to rt_thread_control().
if (thread == NULL) {
rt_thread_t self = rt_thread_self();
+ if (self == RT_NULL) {
+ USB_LOG_ERR("Failed to get current thread\r\n");
+ return;
+ }
rt_thread_control(self, RT_THREAD_CTRL_CLOSE, RT_NULL);Committable suggestion skipped: line range outside the PR's diff.
Summary by CodeRabbit